package com.cuotigou.service.impl;


import com.cuotigou.mapper.SMobileCodeMapper;
import com.cuotigou.pojo.MobileCode;
import com.cuotigou.service.MobileCodeService;
import com.cuotigou.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yangweijian on 2018/1/15.
 */
@Service
@Transactional
public class MobileCodeServiceImpl implements MobileCodeService {

    public static final String SMS_KEY = "bb58e7f24647f1cd9dda6f68c85ce130";

    //短信请求URL
    public static final String SMS_URL = "http://v.juhe.cn/sms/send";

    private static final Logger LOG = LoggerFactory.getLogger(MobileCodeServiceImpl.class);

    @Autowired
    private SMobileCodeMapper sMobileCodeMapper;
    @Override
    public ObjectResult  checkMobileCode(XMLBean xmlBean) {
        MobileCode mobileCode = sMobileCodeMapper.selectCodeByMobile(xmlBean.getMobile());

        Boolean flag = JSMSExample.sendValidSMSCode(mobileCode.getCode(), xmlBean.getCode());
        if(!flag){
            return ObjectResult.build("0","验证码错误");
        }

        return ObjectResult.build("1","正确");
    }

    @Override
    public void insertMobileCode(Map<String, Object> map) {
        sMobileCodeMapper.insertMobileCode(map);
    }

    @Override
    public ObjectResult sms_code(XMLBean xmlBean, String ip)  {
        Map<String,Object> map = new HashMap<String,Object>();

        try {


            String date1 = DateUtil.formatDate();
            String date2 = DateUtil.formatDate(DateUtil.addDateDay(new Date(), 1));
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            long startTime = format.parse(date1).getTime();
            long endTime = format.parse(date2).getTime();

            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("mobile", xmlBean.getMobile());
            Integer count = sMobileCodeMapper.selectMobileCodeCount(map);
            if (count > 10) {
                return ObjectResult.build("0", "短信发送超过限制");
            }
            map.clear();
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("ip", ip);
            count = sMobileCodeMapper.selectMobileCodeCount(map);
            if (count > 20) {
                return ObjectResult.build("0", "短信发送超过限制");
            }

            //发送短信验证码
            String msg_id = JSMSExample.sendSMSCode(xmlBean.getMobile());

            //添加纪录
            map.clear();
            map.put("mobile", xmlBean.getMobile());
            map.put("code", msg_id);
            map.put("send_time", System.currentTimeMillis());
            map.put("ip", ip);
            sMobileCodeMapper.insertMobileCode(map);
            return ObjectResult.build("1", "发送成功");
        }catch (Exception e){
            ExceptionUtil.logError(e,LOG);
            return ObjectResult.build("0", "发送失败");
        }
    }
}
